<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="水票名称">
          <el-input v-model="form.name" placeholder="请输入水票名称"></el-input>
        </el-form-item>
        <el-form-item label="水票类型">
          <el-select
            v-model="form.mobile"
            placeholder="选择水票类型"
            @change="selectChange"
          >
            <el-option
              v-for="(item, index) in Data"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="截止日期">
          <el-date-picker
            v-model="form.time"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
            @change="timeChange"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <!-- <span class="gauge">收入金额: {{ count_price }}元</span> -->
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        border
        style="width: 100%"
        max-height="600"
      >
        <el-table-column label="ID" prop="id" width="55" align="center">
        </el-table-column>
        <el-table-column prop="water_name" align="center" label="水票名称">
        </el-table-column>
        <el-table-column prop="goods_cat_name" align="center" label="水票类型">
        </el-table-column>
        <el-table-column prop="water_number" align="center" label="水票数量">
        </el-table-column>
        <el-table-column align="center" label="水票图片">
          <template slot-scope="scope">
            <el-image
              style="width: 80px; height: 80px"
              :src="scope.row.water_img"
              fit="cover"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="goods_name" align="center" label="商品名称">
        </el-table-column>
        <el-table-column prop="brand_name" align="center" label="水品牌">
        </el-table-column>
        <el-table-column prop="ctime" align="center" label="统计日期">
        </el-table-column>
        <el-table-column prop="buy_count" align="center" label="售票金额(总)">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row)"
              size="mini"
              type="primary"
              >明细</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pageUser">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 150, 200, 300]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="packGit"
        border
        style="width: 100%"
        max-height="600"
      >
        <el-table-column label="ID" prop="id" align="center"> </el-table-column>
        <el-table-column prop="water_name" align="center" label="水票名称">
        </el-table-column>
        <el-table-column prop="goods_cat_name" align="center" label="水票类型">
        </el-table-column>
        <el-table-column prop="package_nember" align="center" label="水票数量">
        </el-table-column>
        <el-table-column prop="water_img" align="center" label="水票图片">
          <template slot-scope="scope">
            <el-image
              style="width: 80px; height: 80px"
              :src="scope.row.water_img"
              fit="cover"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="goods_name" align="center" label="商品名称">
        </el-table-column>
        <el-table-column prop="brand_name" align="center" label="水品牌">
        </el-table-column>
        <el-table-column prop="payed" align="center" label="售票金额">
        </el-table-column>
      </el-table>
      <div class="pageUser">
        <el-pagination
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"
          :current-page="page1"
          :page-sizes="[10, 150, 200, 300]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total1"
        >
        </el-pagination>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  packageCount,
  packageIndex,
  classification,
} from "../../../../utils/api.js";
export default {
  data() {
    return {
      token: sessionStorage.getItem("token"),
      //请求特效
      loading: false,
      //弹窗
      dialogVisible: false,
      form: {
        name: "",
        mobile: "",
        time: "",
      },
      time: "",
      //发送请求时间参数
      //展示数据
      tableData: [
        {
          water_name: "",
          goods_cat_name: "",
          water_number: "",
          id: "",
          brand_name: "",
          goods_name: "",
          water_img: "",
          ctime: "",
          buy_count: "",
          water_cat_id: "",
          ctime: "",
        },
      ],
      //明细
      packGit: [
        {
          water_nameL: "",
          goods_cat_name: "",
          package_nember: "",
          goods_name: "",
          brand_name: "",
          water_img: "",
          ctime: "",
          payed: "",
        },
      ],
      //水票类型
      Data: [
        {
          id: "",
          name: "",
        },
      ],
      page: 1,
      limit: 10,
      total: 0,
      page1: 1,
      limit1: 10,
      total1: 0,
      imgHeader: this.Global.imgHeader,
    };
  },
  methods: {
    //日期
    timeChange(e) {
      console.log(e);
      let list = [];
      e.forEach((item) => {
        let date = new Date(item);
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let h = date.getHours();
        let min = date.getMinutes();
        let s = date.getSeconds();
        month = month < 10 ? "0" + month : month;
        day = day < 10 ? "0" + day : day;
        h = h < 10 ? "0" + h : h;
        min = min < 10 ? "0" + min : min;
        s = s < 10 ? "0" + s : s;
        list.push(
          year + "-" + month + "-" + day + " " + h + ":" + min + ":" + s
        );
      });

      this.time = list[0] + "," + list[1];
      console.log(this.time);
    },
    //查询
    onSubmit(row) {
      this.packageCounts();
    },
    //水票分类
    selectChange() {},
    //分页器
    handleSizeChange(val) {
      this.limit = val;
      this.packageCounts();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.packageCounts();
    },
    handleSizeChange1(val) {
      this.limit1 = val;
      this.packageIndexs();
    },
    handleCurrentChange1(val) {
      this.page1 = val;
      this.packageIndexs();
    },
    // 序号排序
    Method(index) {
      if (this.token < this.limit1) {
        return this.total1 - index;
      } else {
        return this.total1 - (this.page1 - 1) * this.limit1 - index;
      }
    },
    //获取列表数据
    packageCounts() {
      this.loading = true;
      packageCount(
        this.token,
        this.page,
        this.limit,
        this.form.mobile,
        this.time,
        this.form.name
      )
        .then((res) => {
          if (res.data.status) {
            this.tableData = res.data.data.data;
            this.total = res.data.data.count;
            this.tableData.forEach((item) => {
              if (item.water_img) {
                item.water_img1 = item.water_img;
                item.water_img = this.imgHeader + item.water_img;
              }
            });
            let timer = setTimeout(() => {
              this.loading = false;
              clearTimeout(timer);
            }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //明细列表数据
    packageIndexs() {
      this.loading = true;
      packageIndex(this.token, this.page1, this.limit1, this.tableData.id, "")
        .then((res) => {
          if (res.data.status) {
            this.packGit = res.data.data.date;
            this.total1 = res.data.data.count;
            console.log(this.total1);
            this.packGit.forEach((item) => {
              if (item.water_img) {
                item.water_img1 = item.water_img;
                item.water_img = this.imgHeader + item.water_img;
              }
            });
            let timer = setTimeout(() => {
              this.loading = false;
              clearTimeout(timer);
            }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //水票分类
    sdhadj() {
      this.loading = true;
      classification(this.token)
        .then((res) => {
          if (res.data.status) {
            this.Data = res.data.data.data;

            let timer = setTimeout(() => {
              this.loading = false;
              clearTimeout(timer);
            }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/login");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            let timer = setTimeout(() => {
              this.$message.error(res.data.msg);
            }, 1000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //明细
    handleClick(row) {
      let obj = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
      this.tableData.water_cat_id = obj.water_cat_id;
      this.tableData.id = obj.id;
      this.packageIndexs();
    },
    //关闭弹窗
    handleClose(done) {
      done();
      this.packageCounts();
    },
  },
  mounted() {
    this.packageCounts();
    this.sdhadj();
  },
};
</script>

<style scoped>
.gauge {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}
</style>